<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Lib\Category;
class CustomerController extends CommonController {
    protected $tableName = 'Customer';

    public function index() {
        $M = M("Customer");
        $Record = M("Record");

        $talk_time = I('talk_time');
        $talk_time2 = I('talk_time2');
        $lurushijian = I('lurushijian');    //录入时间
        $shangci = I('shangci');    //上次跟进时间

        $act = I('get.act',0);  //全局搜索1
		$user_id = I('user_id',0);
        $source = I('source',0);
        $level = I('level',0);
        $course = I('course',0);
        $rank = I('rank',0);
        $keyword = trim($_GET['keyword']);
        $keyword2 = trim($_GET['keyword2']);
        $keyword3 = trim($_GET['keyword3']);
        $keyword4 = trim($_GET['keyword4']);
        $course_text = trim($_GET['course_text']);


        if(!empty($source)) $map['source'] = $source;
        if(!empty($course)) $map['course'] = $course;
        if(!empty($rank)) $map['level_text'] = $rank;
        if(!empty($keyword)) $map['name'] = array('like','%'.$keyword.'%');
        if(!empty($keyword2)) $map['phone'] = array('like','%'.$keyword2.'%');
        if(!empty($keyword3)) $map['qq'] = array('like','%'.$keyword3.'%');
        if(!empty($keyword4)) $map['address'] = array('like','%'.$keyword4.'%');
        if(!empty($course_text)) $map['course_text'] = array('like','%'.$course_text.'%');
        if(!empty($lurushijian)){
            $s = strtotime($lurushijian);
            $e = strtotime($lurushijian)+86400-1;
            $map['create_time'] = array('between',$s.','.$e);
        }
        if(!empty($shangci)){
            $s2 = strtotime($shangci);
            $e2 = strtotime($shangci)+86400-1;
            $map['talk_time'] = array('between',$s2.','.$e2);
        }

        //查询本月、上月、本周、今天、昨天胡的数据
        switch($talk_time2){
            case 1:
                $startDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
                $endDate = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            break;
            case 2:
                // 昨天
                $startDate = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),date("d"),date("Y"))-1;
            break;
            case 3:
                // 明天
                $startDate = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),date("d")+2,date("Y"))-1;
            break;
            case 4:
                $w = date("w");
                $startDate = mktime(0,0,0,date("m"),date("d")-$w+1,date("Y"));
                $endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
            break;
            case 5:
                // 上周
                $startDate=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
                $endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
            break;
            case 6:
                // 下周
                $startDate=mktime(0,0,0,date('m'),date('d')-date('w')+7+1,date('Y'));
                $endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7+7,date('Y'));
            break;
            case 7:
                // 本月
                $startDate = mktime(0,0,0,date("m"),1,date("Y"));
                $endDate = mktime(23,59,59,date('m'),date('t'),date('Y'))-1;
            break;
            case 8:
                // 上月
                $startDate = mktime(0,0,0,date("m")-1,1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),1,date("Y"))-1;
            break;
            case 9:
                // 下个月
                $startDate = mktime(0,0,0,date("m")+1,1,date("Y"));
                $endDate = mktime(0,0,0,date("m")+2,1,date("Y"))-1;
            break;
            case 10:
                //今年
                $startDate = mktime(0,0,0,1,1,date("Y"));
                $endDate = mktime(0,0,0,1,1,date("Y")+1)-1;
            break;

            default:
                $startDate = 0;
                $endDate = time();
        }
        if(!empty($startDate)){
            $map['talk_time2'] = array('between',$startDate.','.$endDate);
        }


        if($act==0){
            $map['status'] = 1;
            if(!empty($user_id)){
                $map['user_id'] = $user_id;
            }else{
                if($this->uid!=1)
                    $map['user_id'] = $this->uid;
            }
        }

        //默认情况下 已报名不显示在列表上，当搜索关键词不为空或者选择已报名搜索的情况下，可以搜索到
        if(empty($level)){
            $map['level'] = array('neq',4);
        }else{
            $map['level'] = $level;
        }

        //排序处理
        $talk_time2_sort = I('get.talk_time2_sort');
        if(!empty($talk_time2_sort)){
            if($talk_time2_sort == 2){
                $order = 'talk_time2 DESC ';
                $talk_time2_sort = 1;
            }else{
                $talk_time2_sort = 2;
                $order = 'talk_time2 ASC ';
            }
            $this->assign('talk_time2_sort',$talk_time2_sort);
        }

        $create_time_sort = I('get.create_time_sort');
        if(!empty($create_time_sort)){
            if($create_time_sort == 2){
                $order = 'create_time DESC ';
                $create_time_sort = 1;
            }else{
                $create_time_sort = 2;
                $order = 'create_time ASC ';
            }
            $this->assign('create_time_sort',$create_time_sort);
        }

        $sort_level = I('get.sort_level');
        if(!empty($sort_level)){
            if($sort_level == 2){
                $order = 'level DESC ';
                $sort_level = 1;
            }else{
                $sort_level = 2;
                $order = 'level ASC ';
            }
            $this->assign('sort_level',$sort_level);
        }

        $sort_rank = I('get.sort_rank');
        if(!empty($sort_rank)){
            if($sort_rank == 2){
                $order = 'level_text DESC ';
                $sort_rank = 1;
            }else{
                $sort_rank = 2;
                $order = 'level_text ASC ';
            }
            $this->assign('sort_rank',$sort_rank);
        }

        if(empty($order)){
            $order = 'talk_time DESC, id DESC';
        }


        $count = $M->where($map)->count();
        $Page       = new \Think\Page($count,50);
        $showPage = $Page->show();
        $this->assign("page", $showPage);
        $this->assign('count',$count);

        $list = $M->where($map)->order( $order )->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $key => $val) {

            if($val['talk_num']>0){
                $contet = $Record->where( array('item_id'=>$val['id']) )->order( 'id DESC' )->getField('content');
                $list[$key]['content'] = strip_tags($contet);
            }
        }
        $this->assign('list',$list);
        $this->assign('notTime',NOW_TIME);
        $this->assign('act',$act);  //全局搜索

        $this->display('customer');
    }

    public function pool(){
        $M = M("Customer");
        $counselor = I('counselor',0);
        $source = I('source',0);
        $level = I('level',0);
        $keyword = trim($_GET['keyword']);

        if (IS_GET){

            if(!empty($counselor)) $map['counselor'] = $counselor;
            if(!empty($source)) $map['source'] = $source;
            if(!empty($level)) $map['level'] = $level;
            if(!empty($keyword)) $map['name|phone'] = array('like','%'.$keyword.'%');
        }

        $map['status'] = 2;
        $count = $M->where($map)->count();
        $Page       = new \Think\Page($count,50);
        $showPage = $Page->show();
        $this->assign("page", $showPage);
        $list = $M->where($map)->order('update_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function dustbin(){
        $M = M("Customer");
        $counselor = I('counselor',0);
        $source = I('source',0);
        $level = I('level',0);
        $keyword = trim($_GET['keyword']);

        if (IS_GET){

            if(!empty($counselor)) $map['counselor'] = $counselor;
            if(!empty($source)) $map['source'] = $source;
            if(!empty($level)) $map['level'] = $level;
            if(!empty($keyword)) $map['name|phone'] = array('like','%'.$keyword.'%');
        }

        $map['status'] = 0;
        $count = $M->where($map)->count();
        $Page       = new \Think\Page($count,50);
        $showPage = $Page->show();
        $this->assign("page", $showPage);
        $list = $M->where($map)->order('update_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->display();
    }

    // 已报名
    public function deal(){
        $M = M("Customer");
        $Record = M("Record");

        $talk_time = I('talk_time');
        $talk_time2 = I('talk_time2');

        $user_id = I('user_id',0);
        $source = I('source',0);
        $course = I('course',0);
        $rank = I('rank',0);
        $keyword = trim($_GET['keyword']);
        $course_text = trim($_GET['course_text']);

        if (IS_GET){

            if(!empty($counselor)) $map['counselor'] = $counselor;
            if(!empty($source)) $map['source'] = $source;
            if(!empty($keyword)) $map['name|phone'] = array('like','%'.$keyword.'%');
        }

        $map['level'] = 4;
        $map['status'] = 1;
        if(!empty($user_id)){
            $map['user_id'] = $user_id;
        }else{
            if($this->uid!=1)
                $map['user_id'] = $this->uid;
        }
        $count = $M->where($map)->count();
        $Page       = new \Think\Page($count,50);
        $showPage = $Page->show();
        $this->assign("page", $showPage);
        $list = $M->where($map)->order('update_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $key => $val) {

            if($val['talk_num']>0){
                $contet = $Record->where( array('item_id'=>$val['id']) )->order( 'id DESC' )->getField('content');
                $list[$key]['content'] = strip_tags($contet);
            }
        }
        $this->assign('list',$list);
        $this->display();
    }

    public function category() {
        if (IS_POST) {
            echo json_encode(D("Customer")->category());
        } else {
            $this->assign("list", D("Customer")->category());
            $this->display();
        }
    }

    public function add() {
        $Customer = M('Customer');
        $Record = M('Record');

        if (IS_POST) {
            $id        = I('id');
            $data      = I('post.data');
            $talk_time2 = I('post.talk_time2');
            $record_content = I('post.record_content');
            $user_id = $this->uid;

            if(!empty($talk_time2)){
                $data['talk_time2'] = strtotime($talk_time2);    //下次跟进时间
            }

            $data['user_id'] =  $user_id;
            $data['update_time'] = NOW_TIME;

            $data['talk_time'] = $talk_time;    //上次跟进时间
            if($id){
                $map = array();
                $map['phone'] = trim($data['phone']);
                $map['id'] = array('neq',$id);
                $info = $Customer->where($map)->find();
                if(!empty($info)){
                    $this->ajaxReturn( array('status' => 0, 'info' => "该学员已存在，请勿重复添加！") );
                }
                /*if(strtotime($talk_time2)<NOW_TIME){
                    $this->ajaxReturn( array('status' => 0, 'info' => "跟进时间必须大于现在时间！") );
                }*/
                $Customer->where( array('id'=>$id) )->save($data);
                $result = array('status' => 1, 'info' => "操作成功！！", 'url' => U('Customer/index',array('cid'=>$data['cid'])));
            }else{

                $info = $Customer->where( array('phone'=>$data['phone']) )->find();
                if(!empty($info)){
                    $this->ajaxReturn( array('status' => 0, 'info' => "该学员已存在，请勿重复添加！") );
                }

                $data['talk_time'] = NOW_TIME;
                $data['create_time'] = NOW_TIME;
                $data['status'] = 1;
                $item_id = $Customer->add($data);

                //添加通话记录
                if(!empty($record_content)){
                    $data['user_id'] = $user_id;
                    $data['item_id'] = $item_id;
                    $data['progress'] = 1;
                    $data['talktime'] = NOW_TIME;
                    $data['content'] = $record_content;

                    $data['create_time'] = NOW_TIME;
                    $rid = $Record->add($data);
                    if( !empty($rid) ){
                        if(!empty($talk_time2)){
                            $data['talk_time2'] = strtotime($talk_time2);    //跟进时间
                        }
                        $data2['talk_num'] = 1;
                        $data2['talk_time'] = NOW_TIME;
                        $data2['progress'] = 1;
                        $Customer->where( array('id'=>$item_id) )->save($data2);
                    }
                }

                $result = array('status' => 1, 'info' => "添加成功！", 'url' => U('Customer/index'));
            }
            $this->ajaxReturn($result);
        }

        $id = I('id');
        $info = get_info($id);
        $this->assign("info", $info);
        $this->display();
    }

    public function checkCustomerTitle() {
        $M = M("Customer");
        $where = "title='" . $this->_get('title') . "'";
        if (!empty($_GET['id'])) {
            $where.=" And id !=" . (int) $_GET['id'];
        }
        if ($M->where($where)->count() > 0) {
            echo json_encode(array("status" => 0, "info" => "已经存在，请修改标题"));
        } else {
            echo json_encode(array("status" => 1, "info" => "可以使用"));
        }
    }



    //异步获取客户通话记录
    public function get_record_list(){
        $Record = M('Record');
        $item_id = I('get.id');

        $list = $Record->where( array('item_id'=>$item_id) )->order('id DESC')->select();

        $data = '';
        foreach($list as $key=>$val){
            $callDay = date('Ymd') - date('Ymd',$val['talktime']);
            switch($callDay){
                case 0:
                    $callDay =  '今天';
                    break;
                case 1:
                    $callDay =  '昨天';
                    break;
                default:
                    $callDay = $callDay.'天前';
            }

            $data .= '<tr id="content-'.$item_id.'" class="record-'.$item_id.' current-bg">
                <td align="center">'.++$key.'</td>
                <td align="center" style="color:red">'.mydate2($val["talktime"]).'</td>
                <td align="center" style="color:red">'.$callDay.'</td>
                <td align="left" colspan="14" style="color:blue">'.$val['content'].'</td>
                <td align="right" colspan="1" style="padding-right:16px;" >'.date('Y年m月d日 H点i分',$val['talktime']).'</td>
            </tr>';
        }

        $this->ajaxReturn( array('info'=>'获取成功','status'=>1,'data'=>$data));
    }



    public function del() {
        $Customer = M("Customer");
        $Record = M("Record");

        $ids = I('get.id');
        $map['id'] = array('in',$ids);
        /*$list = $Customer->where($map)->select();
        foreach ($list as $key => $val) {
        	$Record->where( array('item_id'=>$val['id']) )->delete();
        }*/

        if ($Customer->where($map)->setField('status',0)) {
            $result = array("status"=>1,"info"=>"成功删除",'url'=>'');
        } else {
            $result = array("status"=>0,"info"=>"删除失败，可能是不存在该ID的记录");
        }
        $this->ajaxReturn($result);
    }

    //彻底删除数据
    public function shanchu() {
        $Customer = M("Customer");
        $Record = M("Record");

        $ids = I('get.id');
        $map['id'] = array('in',$ids);
        if ($Customer->where($map)->delete()) {
            $result = array("status"=>1,"info"=>"成功删除",'url'=>'');
        } else {
            $result = array("status"=>0,"info"=>"删除失败，可能是不存在该ID的记录");
        }
        $this->ajaxReturn($result);
    }

    // 设为私有
    public function owned(){
        $Customer = M("Customer");
        $status = I('get.status');

        $ids = I('get.id');
        $map['id'] = array('in',$ids);

        $data['status'] = $status;
        $data['user_id'] = $this->uid;

        $Customer->where($map)->save($data);
        $result = array("status"=>1,"info"=>"操作成功！",'url'=>'');
        $this->ajaxReturn($result);
    }

    // 添加通话记录
    public function add_record(){
        $Record = M('Record');
        $Customer = M('Customer');

        if( IS_POST ){
            $progress = I('post.progress');   // 跟踪进度
            $item_id = I('post.item_id');
            $talktime2 = I('post.talktime2');
            $content = trim($_POST['content']);
            if(empty($content)){
                $this->ajaxReturn( array('status'=>0, 'info'=>'通话内容不能为空！') );
            }

            $data['user_id'] = $this->uid;
            $data['item_id'] = $item_id;
            $data['progress'] = $progress;
            $data['talktime'] = NOW_TIME;
            $data['content'] = $content;

            $data['create_time'] = NOW_TIME;
            $addId = $Record->add($data);
            if( !empty($addId) ){
                $data2['talk_num'] = $Record->where( array('item_id'=>$item_id) )->count();
                $data2['talk_time'] = NOW_TIME;
                $data2['talk_time2'] = strtotime($talktime2);
                $data2['progress'] = $progress;
                $Customer->where( array('id'=>$item_id) )->save($data2);
            }
            $this->ajaxReturn( array('status'=>1, 'info'=>'添加成功！') );
        }

        $id = I('get.id');
        $info = get_info($id);

        $this->assign('info',$info);
        $this->display();
    }

    public function del_record(){
        $Record = M("Record");
        $Customer = M('Customer');

        $ids = I('get.id');
        $map['id'] = array('in',$ids);

        $record_list = $Record->where($map)->group('item_id')->select();
        if ($Record->where($map)->delete()) {

            //重置通话次数
            foreach ($record_list as $key => $val) {
                $talk_num = $Record->where( array('item_id'=>$val['item_id']) )->count();
                $Customer->where( array('id'=>$val['item_id']) )->setField('talk_num',$talk_num);
            }

            $result = array("status"=>1,"info"=>"成功删除",'url'=>'');
        } else {
            $result = array("status"=>0,"info"=>"删除失败，可能是不存在该ID的记录");
        }
        $this->ajaxReturn($result);
    }

    public function record(){
        $M = D('Record');

        //查询本月、上月、本周、今天、昨天胡的数据
        switch($searchType){
            case 1:
                $startDate = mktime(0,0,0,date("m")-1,1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),1,date("Y"))-1;
            break;
            case 2:
                $w = date("w");
                $startDate = mktime(0,0,0,date("m"),date("d")-$w+1,date("Y"));
                $endDate = time();
            break;
            case 3:
                $startDate = mktime(0,0,0,date("m"),date("d")-1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),date("d"),date("Y"))-1;
            break;
            case 4:
                $startDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
                $endDate = time();
            break;
            case 5:
                $startDate = strtotime($_GET['startTime']);
                $endDate = !empty($_GET['endTime']) ? strtotime($_GET['endTime']) : time();
            break;
            case 6:
                $startDate = mktime(0,0,0,date("m"),1,date("Y"));
                $endDate = time();
            break;
            default:
                $startDate = 0;
                $endDate = time();
        }

        //$where .= "and b.talktime >= ".$startDate." and b.talktime <= ".$endDate." ";
        $where['talktime'] = array('between',$startDate.','.$endDate);

        $user_id = I('get.user_id');
        if(!empty($user_id)&&$this->uid==1){
            $map['user_id'] = $user_id;
        }else{
            if($this->uid!=1)
                $map['user_id'] = $this->uid;
        }
        $map['status'] = 1;
        $count = $M->where($map)->count();
        $Page       = new \Think\Page($count,50);
        $showPage = $Page->show();
        $this->assign("page", $showPage);
        $list = $M->where($map)->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $key => $val) {

        }
        $this->assign('list',$list);
        $this->display();
    }

    public function check_phone(){
        $Customer = M('Customer');
        $phone = I('get.phone');

        $count = $Customer->where( array('phone'=>$phone) )->count();
        if($count){
            $result = array('status'=>'1','info'=>'已存在');
        }else{
            $result = array('status'=>'0','info'=>'可用');
        }
        $this->ajaxReturn($result);
    }

    // 导入
    public function import_customer(){
        $Customer = M('Customer');
        $Record = M('Record');

        if(IS_POST){

            if (!empty($_FILES)) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('xls');// 设置附件上传类型
                $upload->savePath =  '/customer/';// 设置附件上传目录3

                $upload->autoSub  = false;
                $upload->subName  = array('date','Ym');
                $info   =   $upload->uploadOne($_FILES['file']);

                if($info) {
                    $filepath = '/Uploads'.$info['savepath'].$info['savename'];
                    if(!file_exists('./'.$filepath)){
                        $this->ajaxReturn(array('status'=>'0','info'=>'导入失败！','url'=>''));
                    }

                    $list = import_excel('.'.$filepath);
                    unset($list[1]);

                    $successNum = 0;
                    foreach ($list as $key => $val) {
                        // if(empty($val[1])) continue;
                    	/*if($val[2]=='13650250127'){
                    		print_r($val);
                    		exit;
                    	}*/
                        $content = $val['13'];

                        if(!empty($val['11'])){
                            $create_time = xls_time($val['11']);
                        }else{
                            $create_time = NOW_TIME;
                        }

                        if(!empty($val['12'])){
                            $data['talk_time2'] = xls_time($val['12']);
                        }

                        $data['user_id'] = $this->uid;
                        $data['name'] = $val[1];
                        $data['phone'] = $val[2];
                        $data['qq'] = $val[3];
                        $data['address'] = $val[4];
                        $data['level'] = get_level_id($val[5]);
                        $data['level_text'] = get_rank_id($val[6]);

                        $data['course'] = get_course_id($val[7]);
                        $data['course_text'] = $val[8];
                        $data['source'] = get_source_id($val[9]);
                        $data['partner'] = get_partner_id($val[10]);

                        $data['status'] = 1;
                        $data['progress'] = 1;
                        //$data['talk_time'] = $create_time;
                        $data['update_time'] = $create_time;
                        $data['create_time'] = $create_time;


                        if(!empty($content)){
                            $data['talk_num'] = 1;
                        }


                        $id = $Customer->add($data);
                        if($id){
                            if(!empty($content)){

                                $rData['user_id'] = $this->uid;
                                $rData['item_id'] = $id;
                                $rData['progress'] = 1;
                                $rData['content'] = $content;
                                $rData['talktime'] = $create_time;
                                $rData['create_time'] = $create_time;
                                $Record->add($rData);
                            }
                            $successNum++;
                        }
                    }

                    $result = array('status'=>1,'info'=>'成功导入跳'.$successNum.'数据！');
                }else{
                    // 上传错误提示错误信息
                    $result = array('status'=>0,'info'=>$upload->getError());
                }
                $this->ajaxReturn($result);
            }
            exit;
        }
        $this->display();
    }

    // 导出
    public function export_excel(){
        $Customer = M('Customer');
        $Record = M('Record');

        $data[0] = array(
            'id'=>'序号',
            'create_time'=>'录入日期',
            'user_id'=>'所属顾问',
            'source'=>'咨询方式',
            'level'=>'意向级别',
            'level_text'=>'详细级别',
            'name'=>'学员姓名',
            'address'=>'所在地',
            'course'=>'课程项目',
            'course_text'=>'详细课程',
            'phone'=>'联系电话',
            'qq'=>'QQ',
            'talk_time'=>'上次跟进',
            'time_diff'=>'剩余天数',
            'talk_time2'=>'下次跟进',
            'talk_num'=>'跟进次数',
            'content'=>'电话内容',
        );

        $talk_time = I('talk_time');
        $talk_time2 = I('talk_time2');

        $user_id = I('user_id',0);
        $source = I('source',0);
        $level = I('level',0);
        $keyword = trim($_GET['keyword']);


        if(!empty($source)) $map['source'] = $source;
        if(!empty($level)) $map['level'] = $level;
        if(!empty($keyword)) $map['name|phone'] = array('like','%'.$keyword.'%');

        //查询本月、上月、本周、今天、昨天胡的数据
        switch($talk_time2){
            case 1:
                $startDate = mktime(0,0,0,date("m"),date("d"),date("Y"));
                $endDate = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
            break;
            case 2:
                // 明天
                $startDate = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
                $endDate = mktime(0,0,0,date("m"),date("d")+2,date("Y"))-1;
            break;
            case 3:
                $w = date("w");
                $startDate = mktime(0,0,0,date("m"),date("d")-$w+1,date("Y"));
                $endDate = time();
            break;
            case 4:
                // 下周
                $startDate=mktime(0,0,0,date('m'),date('d')-date('w')+7+1,date('Y'));
                $endDate=mktime(23,59,59,date('m'),date('d')-date('w')+7+7,date('Y'));
            break;
            case 5:
                $startDate = mktime(0,0,0,date("m"),1,date("Y"));
                $endDate = mktime(23,59,59,date('m'),date('t'),date('Y'));
            break;
            case 6:
                // 下个月
                $startDate = mktime(0,0,0,date("m"),1,date("Y"));
                $endDate = mktime(0,0,0,date("m")+1,1,date("Y"))-1;
            break;
            default:
                $startDate = 0;
                $endDate = time();
        }
        if(!empty($startDate)){
            $map['talk_time2'] = array('between',$startDate.','.$endDate);
        }

        $map['status'] = 1;
        if(!empty($user_id)){
            $map['user_id'] = $user_id;
        }else{
            if($this->uid!=1)
                $map['user_id'] = $this->uid;
        }



        $field = 'id,create_time,user_id,source,level,level_text,name,address,course,course_text,phone,qq,talk_time,talk_time2,talk_num,user_id';
        $list = $Customer->where($map)->field($field)->select();

        $i = 1;
        foreach($list as $key=>$val){
            $content = '';
            if($val['talk_num']>0){
                $contet = $Record->where( array('item_id'=>$val['id']) )->order( 'id DESC' )->getField('content');
                $contet = strip_tags($contet);

                /*$content_list = $Record->where( array('item_id'=>$val['id']) )->order()->limit()->select();
                foreach ($content_list as $key => $val) {
                    $content .= strip_tags($val['content'])."Chr(10)";
                }*/

            }
            $data[$i]['id'] = $i;
            $data[$i]['create_time'] = mydate2($val['create_time']);
            $data[$i]['user_id'] = get_counselor_name($val['user_id']);
            $data[$i]['source'] = get_source_name($val['source']);
            $data[$i]['level'] = levelArr($val['level']);
            $data[$i]['level_text'] = $val['level_text'];
            $data[$i]['name'] = $val['name'];
            $data[$i]['address'] = $val['address'];
            $data[$i]['course'] = get_course_name($val['course']);
            $data[$i]['course_text'] = $val['course_text'];
            $data[$i]['phone'] = $val['phone'];
            $data[$i]['qq'] = $val['qq'];
            $data[$i]['talk_time'] = mydate2($val['talk_time']);
            $data[$i]['time_diff'] = time_diff($vo['talk_time'],$vo['talk_time2']);
            $data[$i]['talk_time2'] = mydate2($val['talk_time2']);
            $data[$i]['talk_num'] = $val['talk_num'];
            $data[$i]['content'] = $contet;

            $i++;
        }

        $filename = '学员资料'.date('his').'.xls';

        create_xls($data,$filename);
        exit;
    }


    public function import_xls(){
        $data=import_excel('./a.xls');
        print_r($data);
    }

}
